//输入一个链表的头节点，从尾到头反过来返回每个节点的值（用数组返回）。 
//
// 
//
// 示例 1： 
//
// 输入：head = [1,3,2]
//输出：[2,3,1] 
//
// 
//
// 限制： 
//
// 0 <= 链表长度 <= 10000 
//
// Related Topics 栈 递归 链表 双指针 👍 400 👎 0


package com.leetcode.editor.cn;

import com.leetcode.editor.cn.common.ListNode;

import java.util.Arrays;

public class PJianZhiOffer06CongWeiDaoTouDaYinLianBiaoLcof {
    public static void main(String[] args) {
        Solution solution = new PJianZhiOffer06CongWeiDaoTouDaYinLianBiaoLcof().new Solution();
        ListNode listNode = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(4);
        listNode.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode;
        int[] ints = solution.reversePrint(listNode);
        System.out.println(Arrays.toString(ints));

    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     * int val;
     * ListNode next;
     * ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public int[] reversePrint(ListNode head) {
            if (head == null) {
                return new int[0];
            }
            ListNode curr = head;
            ListNode pre = null;
            int count = 0;
            while (curr != null) {
                //临时变量
                ListNode temp = curr.next;
                curr.next = pre;
                pre = curr;
                curr = temp;
                count++;
            }
            int[] res = new int[count];
            int i = 0;
            while (pre != null) {
                res[i++] = pre.val;
                pre = pre.next;
            }
            return res;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}